package javax.generic.swing;

import java.awt.Dialog;

import javax.generic.persistance.AbstractPersistableEntity;
import javax.generic.persistance.GenericDao;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class EditEntityForm<T extends AbstractPersistableEntity> extends VisualEditor<T> {

	private static final long serialVersionUID = 4475114908463164513L;

	private GenericDao dao;
	private JTable table;

	public EditEntityForm(T object, GenericDao dao, JTable table, Dialog owner) {
		super(object, owner);
		this.dao = dao;
		this.table = table;
	}

	@Override
	public void onOk(T object) {
		dao.update(object);

		final int index = table.getSelectedRow();

		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		Object[] row = object.getRow();
		for (int i = 0; i < row.length; i++) {
			dtm.setValueAt(row[i], index, i);
		}
		dtm.fireTableDataChanged();
		table.setRowSelectionInterval(index, index);
		super.onOk(object);
	}
}
